package concurrentV;

public class TwoThreadPrintera1 {
    private static Object lock = new Object();
    private static Boolean isLetter = true;// isLetter == true 打印字母


    private void printerNum() {
        for (int i = 1; i <= 26; i++) {
            synchronized (lock) {
                if (isLetter) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + ":" + i);
                isLetter = !isLetter;
                lock.notifyAll();
            }
        }
    }

    private void printLetter() {
        for (int i = 0; i < 26; i++) {
            synchronized (lock) {
                if (!isLetter) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                char c = (char) ('a' + i);
                System.out.println(Thread.currentThread().getName() + ":" + c);
                isLetter = !isLetter;
                lock.notifyAll();
            }
        }
    }

    public static void main(String[] args) {
        TwoThreadPrintera1 twoThreadPrintera1 = new TwoThreadPrintera1();
        new Thread(() -> twoThreadPrintera1.printLetter()).start();
        new Thread(() -> twoThreadPrintera1.printerNum()).start();
    }
}
